<?php

namespace App\Admin\Controllers;

use App\Admin\Renderable\PaperFeedbacksSelect;
use App\Admin\Repositories\PaperFeedback;
use App\Models\PaperLibrary;
use Dcat\Admin\Admin;
use Dcat\Admin\Form;
use Dcat\Admin\Form\NestedForm;
use Dcat\Admin\Grid;
use Dcat\Admin\Layout\Content;
use Dcat\Admin\Layout\Row;
use Dcat\Admin\Controllers\AdminController;
use Dcat\Admin\Show;
use Illuminate\Support\Facades\URL;

class PaperFeedbackController extends AdminController
{
    public $paperLibrarieId = 0;

    public function __construct(){
//        $url = URL::full();
//        preg_match('/paper_feedback([\s\S]*?)\/manage/', $url, $urlData);
//        $this->paperLibrarieId = $urlData[1];
//        if (isset($_GET['paper_librarie_id'])) {
            $this->paperLibrarieId = $_GET['paper_librarie_id'];
//        }
    }

    public function index(Content $content)
    {
        return $content
            ->title('题库')
            ->description('列表')
            ->breadcrumb('题库')
            ->body(function (Row $row) {
//                $row->column(12, function (Column $column) {
//                    $form = new WidgetForm();
//                    $form->action(admin_url('paper_feedback'));
//
//                    $form->textarea('subject_title')->required();
//                    $form->table('subject_select', function (NestedForm $table) {
//                        $numArray = ['A'=>'A', 'B'=>'B', 'C'=>'C', 'D'=>'D', 'E'=>'E', 'F'=>'F'];
//                        $table->select('num', '选项')->options($numArray)->default('A', true);
//                        $table->text('content', '内容');
//                    })->required();
//
//                    $form->hidden('_token')->default(csrf_token());
//                    $form->width(9, 2);
//                    $column->append(Box::make('新增', $form));
//                });
                $row->column(12, $this->grid());

            });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new PaperFeedback(), function (Grid $grid) {
//            $grid->model()->setConstraints([
//                'paper_librarie_id' => $this->paperLibrarieId,
//            ]);

            $libaryData = PaperLibrary::find($this->paperLibrarieId);

            $grid->model()->where('paper_librarie_id', $this->paperLibrarieId);
            $grid->model()->orderBy('subject_no', 'asc');


            $grid->column('id')->sortable()->width('5%');
            $grid->column('subject_no', '题号')->sortable();
            $grid->column('subject_title')->width('35%');

            $grid->column('subject_select', '选项')
                ->display('查看')
                ->expand(function () {
                    return PaperFeedbacksSelect::make(['id' => $this->id]);
                })
                ->width('10%');

            $grid->column('is_multiple', '类型')->display(function ($is_multiple) {
                return $is_multiple ? '多选' : '单选';
            })->width('10%');

            Form::dialog('编辑')
                ->click('.edit-form')
                ->success('Dcat.reload()');
            Form::dialog('新增')
                ->click('.create-form')
                ->url('paper_feedback/create?paper_librarie_id='.$this->paperLibrarieId) // 表单页面链接，此参数会被按钮中的 “data-url” 属性替换。。
                ->success('Dcat.reload()');

            $grid->tools('<button class="btn btn-primary pull-right create-form">新增</button>');
            $paperLibrarieId = $this->paperLibrarieId;
            $grid->column('操作')->display(function () use ($paperLibrarieId, $libaryData) {
                $editPage = admin_base_path('paper_feedback/'.$this->id.'/edit?paper_librarie_id='.$paperLibrarieId);
                $delPage = admin_base_path('paper_feedback/'.$this->id.'?paper_librarie_id='.$paperLibrarieId);
                $html = '';
                if ($libaryData->publish_state == 0) {
                    $html .= "<span class='btn btn-primary edit-form' data-url='{$editPage}' style='margin-right: 10px;'><i class='feather icon-edit-1'></i> 编辑</span>";
                }
                $html .= "<span class='btn btn-primary' data-url='{$delPage}' data-message='{$this->subject_title}' data-action='delete' style='cursor: pointer' class='grid-row-action-61c3d1d623907'><i class='feather icon-trash'></i> 删除</span>";
                return $html;

            })->width('20%');

            $grid->disableCreateButton();
            $grid->disableActions(); // 隐藏操作栏
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    public static function detail($id)
    {
        return Show::make($id, new PaperFeedback(), function (Show $show) {
            $show->field('id');
            $show->field('paper_librarie_id');
            $show->field('subject_no');
            $show->field('subject_title');
            $show->field('subject_select');
            $show->field('region_id');
            $show->field('created_at');
            $show->field('updated_at');
        });
    }

    public function edit($id, Content $content)
    {
        return $content
            ->title($this->title())
            ->description($this->description()['edit'] ?? trans('admin.edit'))
            ->body($this->form($id)->edit($id));
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form($pfid = 0)
    {
        return Form::make(new PaperFeedback(), function (Form $form) use ($pfid) {
            if ($pfid) {
                $form->action(admin_url('paper_feedback/'.$pfid.'?paper_librarie_id='.$this->paperLibrarieId));
            } else {
                $form->action(admin_url('paper_feedback?paper_librarie_id='.$this->paperLibrarieId));
            }

            if ($pfid == 0) {
                $count = \App\Models\PaperFeedback::where('paper_librarie_id', $this->paperLibrarieId)->count();
                $form->number('subject_no')->value($count + 1);
            } else {
                $form->number('subject_no');
            }

            $form->radio('is_multiple', '选择类型')->options([0 => '单选', 1 => '多选'])->default(0);
            $form->textarea('subject_title')->required();
            $form->table('subject_select', function (NestedForm $table) {
                $numArray = ['A'=>'A', 'B'=>'B', 'C'=>'C', 'D'=>'D', 'E'=>'E', 'F'=>'F', 'G'=>'G', 'H'=>'H', 'I'=>'I', 'J'=>'J', 'K'=>'K'];
                $table->select('num', '选项')->options($numArray)->default('A', true);
                $table->text('content', '内容');
            });
        
            $form->display('created_at');
            $form->display('updated_at');
            $form->hidden('paper_librarie_id');

            $paperLibrarieId = $this->paperLibrarieId;
            $form->submitted(function ($form) use ($paperLibrarieId) {
                $form->paper_librarie_id = $paperLibrarieId;
            });
        });
    }
}
